<script setup>
  import { defineProps, onMounted, ref } from 'vue'
  import { http } from '@/utils/http.js'
  import { onLoad, onShow } from '@dcloudio/uni-app'
  import { parseTime } from '@/utils/index.js'

  // 我的发布列表
  const keywords = ref('')
  const isDown = ref(-1)
  const currentPage = ref(1)
  const sendList = ref([])
  async function getSendList() {
    const res = await http.request({
      url: '/api/bidding/myBidding',
      method: 'POST',
      data: {
        page: currentPage.value,
        limit: 10,
        keywords: keywords.value,
        status: isDown.value,
      },
    })
    sendList.value = [...sendList.value, ...res.data]

    sendList.value.forEach((item) => {
      item.goods.forEach((goods) => {
        const currentTimestamp = Math.floor(Date.now() / 1000)
        let daysShort = goods.pass_time - currentTimestamp
        goods.passDay = Math.floor(daysShort / (24 * 60 * 60))
      })
    })
  }

  //搜索框
  function searchSend() {
    currentPage.value = 1
    sendList.value = []
    getSendList()
  }

  //分类
  function chooseCategary(type) {
    if (isDown.value == type) {
      return
    }
    isDown.value = type
    sendList.value = []
    currentPage.value = 1
    getSendList()
  }

  //触底
  function sendListLower() {
    ++currentPage.value
    getSendList()
  }

  //查看出价
  function checkOffer(id) {
    uni.navigateTo({
      url: `/transactionPage/checkoffer/index?id=${id}`,
    })
  }

  function checkOfferDetail(id) {
    uni.navigateTo({
      url: `/transactionPage/offersuccess/index?id=${id}`,
    })
  }

  //商品详情弹出
  const goodspopup = ref()
  const goodsInfo = ref({
    goods_name: '',
    img: '',
  })
  function checkgoods(item) {
    console.log(item)
    goodsInfo.value = item
    goodspopup.value.open('bottom')
  }

  onMounted(() => {
    currentPage.value = 1
    isDown.value = -1
    sendList.value = []
    getSendList()
  })
</script>

<template>
  <view>
    <view class="search">
      <uni-search-bar
        class="uni-mt-10"
        radius="100"
        placeholder="搜索..."
        clearButton="auto"
        cancelButton="none"
        @confirm="searchSend"
        @clear="searchSend"
        v-model="keywords"
        style="width: 100%"
      />

      <!-- <view class="avatar">
				<image
					style="width: 76rpx; height: 76rpx; border-radius: 50%"
					src="../../static/draw.png"
					mode=""
				></image>
				<view class="world-bar"> 发布 </view>
			</view> -->
    </view>
    <view class="screen">
      <view class="screen-left"> </view>
      <view class="screen-right">
        <view
          class="screen-right-item"
          :class="{ '.screen-right-item-active': isDown == 1 }"
          @click="chooseCategary(1)"
        >
          竞拍中
        </view>
        <view
          class="screen-right-item"
          style="width: 216rpx"
          :class="{ '.screen-right-item-active': isDown == 2 }"
          @click="chooseCategary(2)"
        >
          已完成/待结算
        </view>
        <view
          class="screen-right-item"
          :class="{ '.screen-right-item-active': isDown == 3 }"
          @click="chooseCategary(3)"
        >
          已失效
        </view>
      </view>
    </view>
    <scroll-view
      scroll-y="true"
      class="scroll-Y"
      style="height: 80vh"
      @scrolltolower="sendListLower()"
    >
      <view class="send-content">
        <view
          class="send-content-item"
          v-for="(item, index) in sendList"
          :key="'send' + index"
        >
          <view class="send-content-item-top">
            <view class="send-content-item-top-left">
              <view
                class=""
                style="
                  position: relative;
                  width: 88rpx;
                  height: 88rpx;
                  margin-right: 22rpx;
                "
              >
                <image
                  :src="item.himg"
                  style="width: 88rpx; height: 88rpx; border-radius: 50%"
                  mode=""
                ></image>
                <!-- <view
									class="back"
									style="
										position: absolute;
										bottom: 0;
										left: 50%;
										transform: translateX(-50%);
										width: 64rpx;
										height: 28rpx;
										border-radius: 14rpx;
										font-size: 16rpx;
										line-height: 28rpx;
										text-align: center;
									"
								>
									发布
								</view> -->
              </view>
              <view class="" style="font-size: 30rpx; font-weight: 700">
                {{ item.nickname }}
              </view>
            </view>
            <view
              class="top-time"
              style="font-size: 22rpx; color: #9b9b9d"
              v-if="item.status == 1 && item.is_lock == 0"
            >
              {{ parseTime(item.add_time) }}
            </view>
            <image
              v-else-if="item.status == 1 && item.is_lock == 1"
              src="https://ojqn.wm2177.com/wechat_imgs/transition/deal-wait.png"
              mode=""
              style="width: 100rpx; height: 100rpx; margin-right: 30rpx"
            ></image>
            <image
              v-else-if="item.status == 2"
              src="https://ojqn.wm2177.com/wechat_imgs/transition/deal-down.png"
              mode=""
              style="width: 100rpx; height: 100rpx; margin-right: 30rpx"
            ></image>
            <image
              v-else-if="item.status == 3"
              src="https://ojqn.wm2177.com/wechat_imgs/transition/deal-pass.png"
              mode=""
              style="width: 100rpx; height: 100rpx; margin-right: 30rpx"
            ></image>
          </view>
          <view class="send-content-item-center">
            <scroll-view
              class="scroll-view_H"
              scroll-x="true"
              style="height: 150rpx; width: 100%"
            >
              <view class="scroll-view_H-list">
                <view
                  class="scroll-view_H-item"
                  style="position: relative"
                  v-for="goods in item.goods"
                  :key="'goods' + goods.id"
                  @click="checkgoods(goods)"
                >
                  <view v-if="goods.goods_num" class="goods_num" style="">
                    x{{ goods.goods_num }}
                  </view>
                  <image
                    :src="goods.img"
                    style="width: 130rpx; height: 130rpx; display: block"
                    mode=""
                  ></image>
                  <view class="presare" v-if="goods.type == 1"> 预售 </view>
                  <!-- 		<view v-else class="passTime">
										<image
											src="../../../static/pass.png"
											style="width: 16rpx; height: 16rpx"
											mode=""
										></image>
										<view
											class=""
											style="
												font-size: 12rpx;
												color: #fff;
											"
										>
											{{ goods.passDay }}天后自动发货
										</view>
									</view> -->
                </view>
              </view>
            </scroll-view>
            <view class="total-goods"> 共{{ item.num }}件 </view>
          </view>
          <view class="send-content-item-bottom">
            <view class="send-content-item-bottom-left">
              <!-- <view class="send-content-item-bottom-left-item">
								<image
									src="@/static/start-price.png"
									style="
										width: 42rpx;
										height: 42rpx;
										margin-right: 10rpx;
									"
									mode=""
								></image>
								<view
									class=""
									style="font-size: 20rpx; font-weight: 700"
								>
									{{ item.min_price }}
								</view>
							</view> -->
              <view
                class="send-content-item-bottom-left-item"
                v-if="item.is_buy == 1"
              >
                <image
                  src="@/static/one-price.png"
                  style="width: 42rpx; height: 42rpx; margin-right: 10rpx"
                  mode=""
                ></image>
                <view class="" style="font-size: 20rpx; font-weight: 700">
                  {{ item.buy_price }}
                </view>
              </view>
              <view class="send-content-item-bottom-left-item">
                <image
                  src="@/static/join-num.png"
                  style="width: 42rpx; height: 42rpx; margin-right: 10rpx"
                  mode=""
                ></image>
                <view class="" style="font-size: 20rpx; font-weight: 700">
                  {{ item.participate_num }}
                </view>
              </view>
            </view>
            <view class="send-content-item-bottom-right">
              <view
                class="price-button button"
                v-if="item.status == 1 && item.is_lock == 0"
                @click="checkOffer(item.id)"
              >
                查看出价
              </view>
              <view
                class="price-button button"
                v-else
                @click="checkOfferDetail(item.id)"
              >
                查看详情
              </view>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 详情弹出层 -->
    <uni-popup ref="goodspopup" type="bottom">
      <view class="goodspopup-content">
        <view class="goods-title">{{ goodsInfo.goods_name }} </view>
        <image
          :src="goodsInfo.img"
          style="width: 660rpx; height: 660rpx"
          mode=""
        ></image>
      </view>
    </uni-popup>
  </view>
</template>

<style lang="scss" scoped>
  .goods_num {
    position: absolute;
    left: 50%;
    margin-left: -70rpx;
    width: 140rpx;
    z-index: 1;
    bottom: -30rpx;
    color: #333;
    height: 36rpx;
    text-align: center;
    line-height: 36rpx;
    border-radius: 100%;
    font-size: 22rpx;
  }
  .search {
    display: flex;
    align-items: center;
    background-color: #fff;
    .avatar {
      position: relative;
      width: 76rpx;
      height: 76rpx;
      border-radius: 50%;
      .world-bar {
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 64rpx;
        height: 28rpx;
        border-radius: 14rpx;
        background-color: $transaction-color;
        text-align: center;
        line-height: 28rpx;
        font-size: 16rpx;
      }
    }
  }
  .screen {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx;
    background-color: #fff;
    .screen-left {
      font-weight: 700;
      font-size: 28rpx;
    }
    .screen-right {
      display: flex;
      align-items: center;
      .screen-right-item {
        width: 130rpx;
        height: 58rpx;
        margin-right: 16rpx;
        box-sizing: border-box;
        background-color: #f7f7f7;
        border-radius: 29rpx;
        text-align: center;
        line-height: 58rpx;
        font-size: 24rpx;
        &:last-child {
          margin-right: 0;
        }
      }
      .screen-right-item-active {
        background-color: $transaction-color !important;
      }
    }
  }
  .send-content {
    padding: 20rpx;
    .send-content-item {
      height: 398rpx;
      margin-bottom: 20rpx;
      padding: 22rpx 18rpx 0 18rpx;
      box-sizing: border-box;
      border-radius: 12rpx;
      background-color: #fff;
      .send-content-item-top {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .send-content-item-top-left {
          display: flex;
          align-items: center;
        }
      }
      .send-content-item-center {
        position: relative;
        height: 200rpx;
        padding: 32rpx 0;
        box-sizing: border-box;
        border-bottom: 6rpx solid #f5f5f5;
        .total-goods {
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          width: 92rpx;
          height: 46rpx;
          background-color: #eeeeee;
          font-size: 26rpx;
          line-height: 46rpx;
          text-align: center;
          color: #9b9b9d;
        }
        .scroll-view_H {
          .scroll-view_H-list {
            display: flex;
            flex-wrap: nowrap;
            .scroll-view_H-item {
              width: 130rpx;
              height: 130rpx;
              margin-right: 20rpx;
              box-sizing: border-box;
              &:last-child {
                margin-right: 0;
              }
              .presare {
                position: absolute;
                top: 0;
                right: 0;
                width: 54rpx;
                height: 28rpx;
                border-radius: 0 10rpx 0 0;
                background-color: rgba(0, 0, 0, 0.5);
                font-size: 20rpx;
                line-height: 28rpx;
                text-align: center;
                color: #fff;
              }
              .passTime {
                position: absolute;
                top: 0;
                left: 0;
                display: flex;
                align-items: center;
                justify-content: center;
                width: 100%;
                height: 28rpx;
                border-radius: 10rpx 10rpx 0 0;
                background-color: rgba(0, 0, 0, 0.5);
              }
            }
          }
        }
      }
      .send-content-item-bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 92rpx;
        .send-content-item-bottom-left {
          display: flex;
          align-items: center;
          .send-content-item-bottom-left-item {
            width: 134rpx;
            display: flex;
            align-items: center;
          }
        }
        .send-content-item-bottom-right {
          .button {
            width: 158rpx;
            height: 58rpx;
            border-radius: 29rpx;
            background-color: $transaction-color;
            text-align: center;
            line-height: 58rpx;
            font-size: 24rpx;
            font-weight: 700;
          }
        }
      }
    }
  }

  .goodspopup-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 886rpx;
    width: 710rpx;
    margin: 0 auto;
    padding-top: 36rpx;
    box-sizing: border-box;
    border-radius: 10rpx 10rpx 0 0;
    background-color: #fff;
    .goods-title {
      margin-bottom: 17rpx;
      font-size: 30rpx;
    }
  }

  ::v-deep .uni-searchbar {
    padding: 0;
    background-color: transparent;
  }
  ::v-deep .uni-searchbar__box {
    width: 100%;
    height: 72rpx !important;
    border: none !important;
    justify-content: start !important;
  }
  .back {
    background-color: $transaction-color;
  }
</style>
